<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <!-- fixed 属性可以使标签固定定位 -->
    <van-nav-bar
      class="page-nav-bar"
      fixed
    >
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道标签页 -->
    <!-- 通过 animated 属性可以开启切换标签内容时的转场动画。
         通过 swipeable 属性可以开启滑动切换标签页。
         swipe-threshold滚动阈值，标签数量超过阈值且总宽度超过标签栏宽度时开始横向滚动 默认初始值为5
     -->
    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
      :swipe-threshold='3'
    >
      <van-tab
        v-for="channels in channels"
        :key="channels.id"
        :title="channels.name"
      >
        <article-list :channel="channels"></article-list>
      </van-tab>
      <div
        slot="nav-right"
        class="placeholder"
      ></div>
      <!-- 在点击编辑频道时 显示弹出层 -->
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChenneIEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道标签页 -->
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChenneIEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <!-- 子传父 -->
      <channel-edit
        :mychannels='channels'
        :active='active'
        @update-active="onUpdateActive"
      />
    </van-popup>
    <!-- /频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user' // 获取用户信息接口
import ArticleList from './components/article-List' // 展示主体内容部分
import ChannelEdit from './components/channel-edit.vue' // 编辑频道页面
import { mapState } from 'vuex'
import { getItem } from '@/utils/storeag'
import { TOUTIAO_CHANNELS } from '@/utils/conststants'
export default {
  name: 'homePage',
  // 声明组件
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0, // 控制 页码
      channels: [], // 定义空数组  用于接收数据 实现循环遍历的功能
      isChenneIEditShow: false // 控制显示弹出层  默认为不显示
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    // 自定义方法在页面加载时  获取数据  在created中声明方法 在methods中使用方法
    // 可以方便复用  代码简洁清晰 不会狠笼统
    this.loadChannels()
  },
  mounted () { },
  methods: {
    // 使用方法 loadChannels
    async loadChannels () {
      try {
        // 对象结构 拿到data
        // const { data } = await getUserChannels()
        // // 将拿到的数据赋值给 channels 这个数组 在利用数组循环遍历数据到页面上
        // this.channels = data.data.channels
        // 声明
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          const loadChannels = getItem(TOUTIAO_CHANNELS)
          if (loadChannels) {
            channels = loadChannels
          } else {
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        // 如果发生错误  打印如下结果
        // console.log('获取频道数据失败')
        this.$toast('获取频道数据失败')
      }
    },
    // 控制弹出层 的显示
    onUpdateActive (index, isChenneIEditShow = true) {
      // 用户点击时 将active的索引赋值给当前点击的索引
      this.active = index
      // 然后关闭弹出层
      this.isChenneIEditShow = isChenneIEditShow
    }
  }
}
</script>

<style scoped lang='less'>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
      height: 82px;
    }
    // Tab 标签页
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }
  // 汉堡
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    background-color: rgba(255, 255, 255, 0.902);
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
